//https://leetcode.cn/problems/relative-sort-array/description/

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        int len = *max_element(arr1.begin(), arr1.end());
        vector<int> v;
        vector<int> count(len + 1);
        for (auto e : arr1)
        {
            count[e]++;
        }

        for (size_t i = 0; i < arr2.size(); i++)
        {
            for (size_t j = 0; j < count[arr2[i]]; j++)
            {
                v.push_back(arr2[i]);
            }
            count[arr2[i]] = 0;
        }

        for (size_t i = 0; i < len + 1; i++)
        {
            for (size_t j = 0; j < count[i]; j++)
                v.push_back(i);
        }


        return v;

    }
};